package com.esm.serviceimpl;

import com.esm.dao.FuncDao;
import com.esm.daoimpl.FuncDaoImpl;


import com.esm.service.FuncService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 
 * @author 王文成
 *
 */
public class FuncServiceImpl implements FuncService{

FuncDao adao = new FuncDaoImpl();
	
	
	public JSONArray getRoleAuthorityByRidPid(int rid, int fpid) 
	{
		String fids = adao.findRoleFidsByrid(rid);
		JSONArray  jao = adao.findUserFuncsByFidsPid(fids, fpid);
		for(int i=0;i<jao.size();i++)
		{
			JSONObject jo = jao.getJSONObject(i);
			if(jo.get("state").equals("open"))
			{
				continue;
			}
			else
			{
				JSONArray  childrens = this.getRoleAuthorityByRidPid(rid, jo.getInt("id"));
				jo.put("children", childrens);
			}
		}
		return jao;
	}


	public JSONArray getRoleAuthorityByRname(String rname) {
		// TODO Auto-generated method stub
		JSONArray jay=new JSONArray();
		String rfids=adao.findRoleFidsByRname(rname);
		jay=adao.findFuncsByFids(rfids);
		JSONArray allfuncs=adao.findAllFuncs();
		for (int i = 0; i < allfuncs.size(); i++) {
			for (int j = 0; j < jay.size(); j++) {
				JSONObject jo=jay.getJSONObject(j);
				JSONObject alljo=allfuncs.getJSONObject(i);
				if(jo.get("text").equals(alljo.get("text"))){
					alljo.put("isselect", 1);
					break;
				
					
				}
				
			}
			
		}
		return allfuncs;
	}


	public boolean updateRoleFids(String rname, String fids) {
		// TODO Auto-generated method stub
		return adao.updateFidsByRname(rname, fids);
	}


	
	



}
